package com.joven.shelltest.controller;

import com.joven.shelltest.entity.SysUserModel;
import com.joven.shelltest.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.util.List;

/**
 * @Author wzj
 * @Date 2023-10-10 09:37
 **/
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/webflux")
public class WebFluxController {

    @GetMapping("/test")
    public Mono<ResponseEntity<String>> helloWorld() {
        return Mono.just(ResponseEntity.ok("Hello java"));
    }

    @GetMapping("/test/route")
    public Mono<ResponseEntity<String>> filter(String name) {
        return Mono.just(ResponseEntity.ok(name));
    }

    private final SysUserService sysUserService;

    @GetMapping("/testUser")
    public Flux<List<SysUserModel>> getUser() {
        return Flux.just(sysUserService.getUsrByStatus(1));
    }

    @GetMapping(value = "/hello", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> hello() {
        return Flux.just("Hello", "World")
                .delayElements(Duration.ofSeconds(4))
                .map(String::toUpperCase);
    }

    /**
     * Mono 表示一个包含单个元素的响应式序列，Flux 表示一个包含多个元素的响应式序列。
     *
     * @param name 姓名
     * @return /
     */
    @GetMapping(value = "/helloPath/{name}", produces = MediaType.TEXT_PLAIN_VALUE)
    public Mono<String> hello(@PathVariable String name) {
        return Mono.just("Hello " + name);
    }
}
